* ==============================================================================
* ==============================================================================
* SSF:  	SSF_Code.do
* Version:	May 2024
* ==============================================================================
capture cd "AK_TS\SSF\Data"
use "SSF_Dataset.dta", clear
* ==============================================================================
* Generate variables
* ==============================================================================
* ==============================================================================
encode cname, gen(cid)
xtset cid year
replace debtserv_tot = debtserv_tot/1000000000
lab var debtserv_tot "PPG, total debt service (billions USD)"
gen dsrtot_gdp = 100*debtserv_tot/ngdpd_Oct2023
lab var dsrtot_gdp "PPG, total debt service (% of GDP)"
bysort cname (income): replace income = income[_N]
egen m_hic_ggx_ngdp = mean(ggx_ngdp) if income=="High", by(year)
egen m_umic_ggx_ngdp = mean(ggx_ngdp) if income=="Upper-middle", by(year)
egen m_lmic_ggx_ngdp = mean(ggx_ngdp) if income=="Lower-middle", by(year)
egen m_lic_ggx_ngdp = mean(ggx_ngdp) if income=="Low", by(year)
gen initiation = date(arrangementdate, "MDY")
format %td initiation
gen ini_year = year(initiation)
gen ini_month = month(initiation)
xtset cid year
foreach branch in currexp_nc_ wages_nc_ currexp_gdp_ wages_gdp_ deflator_ {
    gen `branch'rn = `branch'r0
    replace `branch'rn = `branch'r1 if !missing(`branch'r1)
    replace `branch'rn = `branch'r2 if !missing(`branch'r2)
    replace `branch'rn = `branch'r3 if !missing(`branch'r3)
    replace `branch'rn = `branch'r4 if !missing(`branch'r4)
    replace `branch'rn = `branch'r5 if !missing(`branch'r5)
    replace `branch'rn = `branch'r6 if !missing(`branch'r6)
}
* Find countries that start the fiscal year mid-way
list cname year ssfq2target ssfq3target if ssfq2target>ssfq3target
gen ssfc4target = ssfq4target
* Countries that report by fiscal year rather than calendar year
replace ssfc4target = ssfq2target if cname=="Kenya" | cname=="Uganda" | cname=="Tanzania" | cname=="Barbados" | cname=="Egypt" | cname=="Nepal"
* Ecuador deals with number of familes covered rather than social expenditure
replace ssfc4target = . if cname=="Ecuador"
gen bbc4target = bbq4target
replace bbc4target = bbq2target if cname=="Kenya" | cname=="Uganda" | cname=="Tanzania" | cname=="Barbados" | cname=="Egypt" | cname=="Nepal"
xtset cid year
* ==============================================================================
* Factual statements
* ==============================================================================
* When disaggregated into income groups, upper-middle income countries exhibited debt service costs of 3.7% of GDP in 2020 but this decreased to 2.7% by 2024,
tabstat dsrtot_gdp if income=="Upper-middle", s(n mean) by(year)
* whereas lower-middle income countries have experienced a rapid escalation of these costs, increasing from 2.5% in 2020 to 3.6% of GDP by 2024.
tabstat dsrtot_gdp if income=="Lower-middle", s(n mean) by(year)
* The debt servicing costs of low income countries are rapidly increasing too, more than doubling as a share of GDP from 1.2% in 2020 to 2.5% in 2024.
tabstat dsrtot_gdp if income=="Low", s(n mean) by(year)
* Once the pandemic was in full swing, these countries increased year-on-year government spending by 6.8% of GDP. Subsequently, these expenditures have experienced a year-on-year decline since 2020, and are expected to reach pre-pandemic levels by 2024.
gen Dm_hic_ggx_ngdp = d.m_hic_ggx_ngdp
tabstat m_hic_ggx_ngdp Dm_hic_ggx_ngdp, s(mean) by(year)
* These countries increased government spending by approximately 4.0% of GDP in 2020, and, despite scaling back spending, are expected to remain slightly above their pre-pandemic levels.
gen Dm_umic_ggx_ngdp = d.m_umic_ggx_ngdp
tabstat m_umic_ggx_ngdp Dm_umic_ggx_ngdp, s(mean) by(year)
* In lower-middle income countries, government spending declined through the 2010s and increased by only 1.3% of GDP in 2020, before returning to pre-pandemic levels the following year.
gen Dm_lmic_ggx_ngdp = d.m_lmic_ggx_ngdp
tabstat m_lmic_ggx_ngdp Dm_lmic_ggx_ngdp, s(mean) by(year)
* And in low income countries, spending stagnated in the 2010s before increasing by 1.3% of GDP in 2020. While spending remained higher than pre-pandemic level in 2022, this is expected to return to pre-pandemic levels in 2023.
gen Dm_lic_ggx_ngdp = d.m_lic_ggx_ngdp
tabstat m_lic_ggx_ngdp Dm_lic_ggx_ngdp, s(mean) by(year)
* ==============================================================================
* Figure 1. Government expenditure on health and external debt service as a share of GDP in low- and middle-income countries
* ==============================================================================
xtset cid year
graph bar (mean) gghed_gdp dsrtot_gdp if (year==2010 | year==2015 | year==2020 | year==2021 | year==2024 ) & income!="High" & !missing(income), over(year) graphregion(fcolor(white)) legend(lab(1 Health) lab(2 External debt service)) ylabel(,angle(horizontal)) ytitle("Average government expenditure (% of GDP)") name(g1) title(All low- and middle-income countries)
graph bar (mean) gghed_gdp dsrtot_gdp if (year==2010 | year==2015 | year==2020 | year==2021 | year==2024 ) & (income=="Upper-middle"), over(year) graphregion(fcolor(white)) legend(lab(1 Health) lab(2 External debt service)) ylabel(,angle(horizontal)) ytitle("Average government expenditure (% of GDP)") name(g2) title(Upper-middle income countries)
graph bar (mean) gghed_gdp dsrtot_gdp if (year==2010 | year==2015 | year==2020 | year==2021 | year==2024 ) & (income=="Lower-middle"), over(year) graphregion(fcolor(white)) legend(lab(1 Health) lab(2 External debt service)) ylabel(,angle(horizontal)) ytitle("Average government expenditure (% of GDP)") name(g3) title(Lower-middle income countries)
graph bar (mean) gghed_gdp dsrtot_gdp if (year==2010 | year==2015 | year==2020 | year==2021 | year==2024 ) & (income=="Low"), over(year) graphregion(fcolor(white)) legend(lab(1 Health) lab(2 External debt service)) ylabel(,angle(horizontal)) ytitle("Average government expenditure (% of GDP)") name(g4) title(Low income countries)
grc1leg2 g1 g2 g3 g4, graphregion(fcolor(white)) scale(0.75) ycommon
graph export "Figures\SFF_Figure1.png", replace width(4000)
graph drop _all
* ==============================================================================
* Figure 2. Government expenditure in low, lower-middle, upper-middle, and high income countries
* ==============================================================================
twoway (scatter m_hic_ggx_ngdp year if year>=2010 & year<=2022, sort(year) connect(l) msymbol(i) lwidth(thick)) (scatter m_umic_ggx_ngdp year if year>=2010 & year<=2022, sort(year) connect(l) msymbol(i) lwidth(thick)) (scatter m_lmic_ggx_ngdp year if year>=2010 & year<=2022, sort(year) connect(l) msymbol(i) lwidth(thick)) (scatter m_lic_ggx_ngdp year if year>=2010 & year<=2022, sort(year) connect(l) msymbol(i) lwidth(thick)), graphregion(fcolor(white)) ytitle(Government expenditure (% of GDP)) ylabel(, angle(horizontal)) xtitle("") xlabel(2010(2)2022) legend(label(1 "HICs") label(2 "UMICs") label(3 "LMICs") label(4 "LICs"))
graph export "Figures/SSF_Figure2.png", replace width(4000)
* ==============================================================================
* Figure 3. The evolution of social spending floors
* ==============================================================================
gen ssf_exp = 100*(ssfc4target/currexp_nc_r6)
foreach num of numlist 5(-1)0 {
    replace ssf_exp = 100*(ssfc4target/currexp_nc_r`num') if missing(ssf_exp)
}
sort cid, stable
by cid: gen obsno = _n
by cid : gen countnonmissing = sum(!missing(ssf_exp)) if !missing(ssf_exp)
bysort cid (countnonmissing) : gen ssf_first = ssf_exp[1]
gen nonmissing = !missing(ssf_exp)
bysort cid (nonmissing year) : gen ssf_last = ssf_exp[_N]
gen ssf_diff = ssf_first - ssf_last
list cname year ssfc4target if ssf_diff==0
gen oneyear = cname=="Tanzania" | cname=="Egypt" | cname=="Barbados"
graph dot ssf_first ssf_last if !oneyear, over(cname, sort(ssf_diff) reverse) nofill legend(rows(1) label(1 "Initial year") label(2 "Latest year"))ytitle(Social spending floor (% of current expenditures)) marker(1, msymbol(O)) marker(2, msymbol(D)) graphregion(fcolor(white)) 
graph export "Figures/SSF_Figure3.png", replace width(4000)
* ==============================================================================
* Table 1. Countries, program, and reviews collected
* ==============================================================================
* See SSF_Tables.xlsx
* ==============================================================================
* Table 2. Budget balance conditions in recent IMF programs
* ==============================================================================
xtset cid year
* Convert from millions to billions or from trillions to billions
gen BILbbc4target = bbc4target
replace BILbbc4target = BILbbc4target/1000 if cname=="Ecuador" | cname=="Gambia" | cname=="Jordan" | cname=="Moldova" | cname=="Suriname" | cname=="Zambia" | cname=="Cabo Verde" | cname=="Barbados"
replace BILbbc4target = bbc4target*1000 if cname=="Tanzania"
gen bbinf = f.BILbbc4target/(1 + (f.deflator_rn)/100)
gen bbperch = (bbinf-BILbbc4target)/BILbbc4target
clist cname year bbtype BILbbc4target f.BILbbc4target f.deflator_rn bbinf bbperch if year==ini_year & bbtype!="." & !missing(BILbbc4target) & !missing(f.BILbbc4target), noobs
* ==============================================================================
* Table 3. Social spending floors as share of current spending (initial end-of-year floor)
* ==============================================================================
sort ssf_first
clist cname ssf_first if !missing(ssf_first) & year==2025, noobs
* ==============================================================================
* Table 4. Implementation rates for social spending floors & budget balance conditions, 2020-23
* ==============================================================================
egen ssf_total = rownonmiss(ssfq1target ssfq2target ssfq3target ssfq4target)
egen ssf_found = rownonmiss(ssfq1actual ssfq2actual ssfq3actual ssfq4actual)
foreach q of numlist 1/4 {
    gen ssf_metq`q' = 1 if ssfq`q'actual >= ssfq`q'target & !missing(ssfq`q'actual)
}
egen ssf_met = rownonmiss(ssf_metq1 ssf_metq2 ssf_metq3 ssf_metq4)
foreach var of varlist ssf_total ssf_found ssf_met {
    egen TOT`var' = total(`var'), by(cname)
}
egen bb_total = rownonmiss(bbq1target bbq2target bbq3target bbq4target)
egen bb_found = rownonmiss(bbq1actual bbq2actual bbq3actual bbq4actual)
foreach q of numlist 1/4 {
    gen bb_metq`q' = 1 if bbq`q'actual >= bbq`q'target & !missing(bbq`q'actual) & bbtype=="F"
    replace bb_metq`q' = 1 if bbq`q'actual <= bbq`q'target & !missing(bbq`q'actual) & bbtype=="C"
}
egen bb_met = rownonmiss(bb_metq1 bb_metq2 bb_metq3 bb_metq4)
foreach var of varlist bb_total bb_found bb_met {
    egen TOT`var' = total(`var'), by(cname)
}
sort cname
clist cname TOT* if year==2023 & !missing(arrangementdate), noobs
* ==============================================================================
* Table 5. Margins for meeting social spending floors at end-year
* ==============================================================================
gen ssf_margin = 100*((ssfq4actual - ssfq4target)/ssfq4target) if !missing(ssfq4actual)
replace ssf_margin = 100*((ssfq2actual - ssfq2target)/ssfq2target) if !missing(ssfq2actual) & (cname=="Kenya" | cname=="Uganda")
replace ssf_margin = . if cname=="Ecuador"
sort ssf_margin
* Afghanistan excluded as definition of social spending floors changed between initial program approval and the first review. Calculation of 0.6% instead based on Afghanistan's downwardly revised definition (rather than -42% based on initial definition).
clist cname year ssf_margin if !missing(ssf_margin) & cname!="Afghanistan", noobs
* ==============================================================================